// personal/report/index.js
import {
  toast
} from '../../utils/util'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select: [],
    currentIndex: 0,
    currentCode: '',
    content: '',
    phone: wx.getStorageSync('userInfo').phone,
    inforid: '',
    infortype: '',
    info: {},
    enterprise: '',
    tabIndex: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initInfoType(options)
  },
  initInfoType(options) {
    const index = Number(options.infortype) - 1
    this.setData({
      infortype: options.infortype,
      inforid: options.inforid,
      tabIndex: index.toString(),
      info: JSON.parse(options.info)
    })
    this.setEnterprise()
  },
  setEnterprise() {
    const code = this.data.info.enttype
    if (code) {
      app.$api.common.commonFindName({
          code: code
        })
        .then((s) => {
          const data = s.data
          this.setData({
            enterprise: data.name
          })
        }).catch((e) => {
          toast('请稍后再试', 1000)
        })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCode()
  },
  getCode() {
    const code = '003'
    app.$api.common.commonFind({
      code
    }).then((res) => {
      const data = res.data
      this.setData({
        select: data
      })
    }).catch((res) => {
      toast('请稍后再试', 1000)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindSelect(e) {
    const {
      index,
      code,
      id
    } = e.currentTarget.dataset
    this.setData({
      currentIndex: index,
      currentCode: code
    })
  },
  validate() {
    const {
      content
    } = this.data
    if (content.trim() === '') {
      toast('建议内容不能为空', 1000)
      return false
    }
    return true
  },
  bindSuggestContent(e) {
    const value = e.detail.value
    this.setData({
      content: value
    })
  },
  bindCommit() {
    const {
      inforid,
      infortype
    } = this.data
    const reportcode = this.data.currentCode
    const reportcontent = this.data.content
    const flag = this.validate()
    if (flag) {
      app.$api.report.reportAdd({
        inforid,
        infortype,
        reportcode,
        reportcontent
      }).then((res) => {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success() {
            wx.navigateTo({
              url: '/pages/index/index'
            })
          }
        })
      }).catch((res) => {
        toast('请稍后再试', 1000)
      })
    }
  }
})